/* This is the do-file that contains program "fungstoreresults".
"fungstoreresults" is a program to capture results for both sectors at the same time. The syntax is
fungstoreresults sector estimation-technique place-to-store-estimates RHS-variables
e.g.: fungstoreresults `sector' OLS OLS`SECTOR'DISAGGR`counter' ``SECTOR'DISAGGR'
The program picks up the relevant statistics for the designated estimation technique. 
Different statistics are picked up depending on the estimation technique and depending on the type of model: 
aggregate linear, disaggregate linear, aggregate double-log, disaggregate double-log.
Given the nature of the statistics I pick up after the OLS and FD regression (countries: N_clust) I'm assuming standard errors are clustered.
*/
capture program drop fungstoreresults
program define fungstoreresults
version 10.1
syntax namelist

local sector: word 1 of `namelist'
local esttechn: word 2 of `namelist'
local resultsstorage: word 3 of `namelist'
local RHS: list namelist - sector
local RHS: list RHS - esttechn
local RHS: list RHS - resultsstorage
local lagdep Lpublicexp`sector'FADGDP
local dynamic: list lagdep in RHS /* dynamic is 1 for a dynamic model, 0 otherwise */
local L`sector'TCtotalGDP6 L`sector'TCtotalGDP6
local TClag: list L`sector'TCtotalGDP6 in RHS /* TClag is 1 for a model where the lag of TC has been added, and is 0 otherwise */


/* Display error messages if sector or esttechn is misspecified, or if resultsstorage disagrees with the last place you've stored results in. */
if "`sector'" != "education" & "`sector'" != "health" {
	di as error "Sector incorrectly specified"
	exit 198
}
if "`resultsstorage'" != e(_estimates_name) {
	di as error "You're not storing the results in the right place"
	exit 198
}


/* Specify locals to identify the general type of model. */
local `sector'IPtotalGDP6 `sector'IPtotalGDP6
local `sector'totalGDP6 `sector'totalGDP6
local ln`sector'IPtotalGDP6 ln`sector'IPtotalGDP6
local ln`sector'totalGDP6 ln`sector'totalGDP6
local `sector'IPCRSscaledGDP `sector'IPCRSscaledGDP
local `sector'CRSscaledGDP `sector'CRSscaledGDP

local disaggr`sector': list `sector'IPtotalGDP6 in RHS
local aggr`sector': list `sector'totalGDP6 in RHS
local disaggrln`sector': list ln`sector'IPtotalGDP6 in RHS
local aggrln`sector': list ln`sector'totalGDP6 in RHS
local disaggr`sector'CRSSC: list `sector'IPCRSscaledGDP in RHS
local aggr`sector'CRSSC: list `sector'CRSscaledGDP in RHS


/* Code for disaggregated linear model */
if `disaggr`sector'' == 1 { 
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'SPtotalGDP6 = 0
		estadd scalar fullfungtestSP = ttail(r(df_r) , _b[`sector'SPtotalGDP6]/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'SPtotalGDP6 = 1
		estadd scalar nofungtestSP = ttail(r(df_r) , - (_b[`sector'SPtotalGDP6]-1)/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = -1
		estadd scalar fullfungtestTC = ttail(r(df_r) , (_b[`sector'TCtotalGDP6] + 1)/_se[`sector'TCtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = 0
		estadd scalar nofungtestTC = ttail(r(df_r) , - _b[`sector'TCtotalGDP6]/_se[`sector'TCtotalGDP6]): `resultsstorage'
	}/* end code OLS and FD estimation */ 
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'SPtotalGDP6 = 0
		estadd scalar fullfungtestSP = ttail(r(df_r), _b[`sector'SPtotalGDP6]/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'SPtotalGDP6 = 1
		estadd scalar nofungtestSP = ttail(r(df_r), - (_b[`sector'SPtotalGDP6]-1)/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = -1
		estadd scalar fullfungtestTC = ttail(r(df_r), (_b[`sector'TCtotalGDP6] + 1)/_se[`sector'TCtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = 0
		estadd scalar nofungtestTC = ttail(r(df_r), - _b[`sector'TCtotalGDP6]/_se[`sector'TCtotalGDP6]): `resultsstorage'

		/* Generalised Hausman test: */
		xtreg publicexp`sector'FADGDP `RHS' year1991-year2003 if sample == 1, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}/* end code FE */
	else if "`esttechn'" == "GMM" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar instruments = e(j): `resultsstorage'
		estadd scalar m1_p = e(ar1p): `resultsstorage'
		estadd scalar m2_p = e(ar2p): `resultsstorage'
		estadd scalar hansen_p = e(hansenp): `resultsstorage'
		matrix define A = e(diffsargan)
		estadd scalar Rood_level = A[5,1]: `resultsstorage'
		estadd scalar Rood_lagdepdiff = A[5,2]: `resultsstorage'
		estadd scalar Rood_lagdeplevel = A[5,3]: `resultsstorage'
		estadd scalar Rood_endog = A[5,4]: `resultsstorage'
		estadd scalar Rood_predeterm = A[5,5]: `resultsstorage'

		/* Test for some potentially interesting restrictions */
		test `sector'IPtotalGDP6 = `sector'ONMtotalGDP6
		estadd scalar p_restrictIPONM = r(p): `resultsstorage'

		test `sector'IPtotalGDP6 = `sector'SPtotalGDP6 = `sector'ONMtotalGDP6 
		estadd scalar p_restrictnonTC = r(p): `resultsstorage'		

		if "`sector'" == "education" {
			test generaltotalGDP6 = othersectoreducGDP6
			estadd scalar p_restrictother = r(p): `resultsstorage'
		}
		else if "`sector'" == "health" {
			test generaltotalGDP6 = othersectorhealthGDP6
			estadd scalar p_restrictother = r(p): `resultsstorage'
		}/* End conditionality for extra test */

		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'SPtotalGDP6 = 0
		estadd scalar fullfungtestSP = ttail(r(df_r), _b[`sector'SPtotalGDP6]/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'SPtotalGDP6 = 1
		estadd scalar nofungtestSP = ttail(r(df_r), - (_b[`sector'SPtotalGDP6]-1)/_se[`sector'SPtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = -1
		estadd scalar fullfungtestTC = ttail(r(df_r), (_b[`sector'TCtotalGDP6] + 1)/_se[`sector'TCtotalGDP6]): `resultsstorage'
		test `sector'TCtotalGDP6 = 0
		estadd scalar nofungtestTC = ttail(r(df_r), - _b[`sector'TCtotalGDP6]/_se[`sector'TCtotalGDP6]): `resultsstorage'

		/* If model is dynamic, also do tests and save results for the long-run effect */
		if `dynamic' == 1 {
			if `TClag' == 1 {
				/* first carry out fungibility tests for the lag of TC */ 
				test L`sector'TCtotalGDP6 = -1
				estadd scalar fullfungtestlagTC = ttail(r(df_r), (_b[L`sector'TCtotalGDP6] + 1)/_se[L`sector'TCtotalGDP6]): `resultsstorage'
				test L`sector'TCtotalGDP6 = 0
				estadd scalar nofungtestlagTC = ttail(r(df_r), - _b[L`sector'TCtotalGDP6]/_se[L`sector'TCtotalGDP6]): `resultsstorage'
				/* Now calculate LR effect and carry out LR fungibility tests */
				nlcom (LRSP: _b[`sector'SPtotalGDP6]/(1-_b[Lpublicexp`sector'FADGDP])) /// 
				(LRTC: (_b[`sector'TCtotalGDP6] + _b[L`sector'TCtotalGDP6])/(1-_b[Lpublicexp`sector'FADGDP])), post
			}/* end extra code with added TC lag */
			else {
				nlcom (LRSP: _b[`sector'SPtotalGDP6]/(1-_b[Lpublicexp`sector'FADGDP])) /// 
				(LRTC: _b[`sector'TCtotalGDP6]/(1-_b[Lpublicexp`sector'FADGDP])), post
			}/* end extra code without TC lag */
			foreach type in SP TC {
				scalar define LR`type' = _b[LR`type']
				scalar define LR`type'_se = _se[LR`type']
				estadd scalar LR`type': `resultsstorage' 
				estadd scalar LR`type'_se: `resultsstorage' 
			}
			test _b[LRSP] = 0
			estadd scalar fullfungtestLRSP = ttail(r(df_r), LRSP/LRSP_se): `resultsstorage'
			test _b[LRSP] = 1
			estadd scalar nofungtestLRSP = ttail(r(df_r), - (LRSP-1)/LRSP_se): `resultsstorage'
			test _b[LRTC] = -1
			estadd scalar fullfungtestLRTC = ttail(r(df_r), (LRTC + 1)/LRTC_se): `resultsstorage'
			test _b[LRTC] = 0
			estadd scalar nofungtestLRTC = ttail(r(df_r), - (LRTC/LRTC_se)): `resultsstorage'
		}/* end code for dynamic model */
		drop sample
	}/* end code GMM estimation technique */ 
	else {
	di as error "Estimation technique ill-specified"
	exit 198
	}
}/* end code disaggregate linear model */


/* Code for disaggregated linear model scaled CRS variables */
else if `disaggr`sector'CRSSC' == 1 { 
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'SPCRSscaledGDP = 0
		estadd scalar fullfungtestSP = ttail(r(df_r) , _b[`sector'SPCRSscaledGDP]/_se[`sector'SPCRSscaledGDP]): `resultsstorage'
		test `sector'SPCRSscaledGDP = 1
		estadd scalar nofungtestSP = ttail(r(df_r) , - (_b[`sector'SPCRSscaledGDP]-1)/_se[`sector'SPCRSscaledGDP]): `resultsstorage'
		test `sector'TCCRSscaledGDP = -1
		estadd scalar fullfungtestTC = ttail(r(df_r) , (_b[`sector'TCCRSscaledGDP] + 1)/_se[`sector'TCCRSscaledGDP]): `resultsstorage'
		test `sector'TCCRSscaledGDP = 0
		estadd scalar nofungtestTC = ttail(r(df_r) , - _b[`sector'TCCRSscaledGDP]/_se[`sector'TCCRSscaledGDP]): `resultsstorage'
	}/* end code OLS and FD estimation */ 
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'SPCRSscaledGDP = 0
		estadd scalar fullfungtestSP = ttail(r(df_r), _b[`sector'SPCRSscaledGDP]/_se[`sector'SPCRSscaledGDP]): `resultsstorage'
		test `sector'SPCRSscaledGDP = 1
		estadd scalar nofungtestSP = ttail(r(df_r), - (_b[`sector'SPCRSscaledGDP]-1)/_se[`sector'SPCRSscaledGDP]): `resultsstorage'
		test `sector'TCCRSscaledGDP = -1
		estadd scalar fullfungtestTC = ttail(r(df_r), (_b[`sector'TCCRSscaledGDP] + 1)/_se[`sector'TCCRSscaledGDP]): `resultsstorage'
		test `sector'TCCRSscaledGDP = 0
		estadd scalar nofungtestTC = ttail(r(df_r), - _b[`sector'TCCRSscaledGDP]/_se[`sector'TCCRSscaledGDP]): `resultsstorage'

		/* Generalised Hausman test: */
		xtreg publicexp`sector'FADGDP `RHS' year1991-year2003 if sample == 1, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}/* end code FE */
	else {
	di as error "Estimation technique ill-specified"
	exit 198
	}
}/* end code disaggregate linear model scaled CRS variables */


/* Aggregate linear model: */
else if `aggr`sector'' == 1 { 
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'totalGDP6 = 0
		estadd scalar fullfungtest = ttail(r(df_r) , _b[`sector'totalGDP6]/_se[`sector'totalGDP6]): `resultsstorage'
		test `sector'totalGDP6 = 1
		estadd scalar nofungtest = ttail(r(df_r) , - (_b[`sector'totalGDP6]-1)/_se[`sector'totalGDP6]): `resultsstorage'
	}
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'totalGDP6 = 0
		estadd scalar fullfungtest = ttail(r(df_r), _b[`sector'totalGDP6]/_se[`sector'totalGDP6]): `resultsstorage'
		test `sector'totalGDP6 = 1
		estadd scalar nofungtest = ttail(r(df_r), - (_b[`sector'totalGDP6]-1)/_se[`sector'totalGDP6]): `resultsstorage'
		/* Generalised Hausman test: */
		xtreg publicexp`sector'FADGDP `RHS' year1991-year2003 if sample == 1, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}
	else {
		di as error "Estimation technique ill-specified"
		exit 198
	}
}


/* Aggregate linear model scaled CRS variables: */
else if `aggr`sector'CRSSC' == 1 { 
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'CRSscaledGDP = 0
		estadd scalar fullfungtest = ttail(r(df_r) , _b[`sector'CRSscaledGDP]/_se[`sector'CRSscaledGDP]): `resultsstorage'
		test `sector'CRSscaledGDP = 1
		estadd scalar nofungtest = ttail(r(df_r) , - (_b[`sector'CRSscaledGDP]-1)/_se[`sector'CRSscaledGDP]): `resultsstorage'
	}
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		test `sector'CRSscaledGDP = 0
		estadd scalar fullfungtest = ttail(r(df_r), _b[`sector'CRSscaledGDP]/_se[`sector'CRSscaledGDP]): `resultsstorage'
		test `sector'CRSscaledGDP = 1
		estadd scalar nofungtest = ttail(r(df_r), - (_b[`sector'CRSscaledGDP]-1)/_se[`sector'CRSscaledGDP]): `resultsstorage'
		/* Generalised Hausman test: */
		xtreg publicexp`sector'FADGDP `RHS' year1991-year2003 if sample == 1, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}
	else {
		di as error "Estimation technique ill-specified"
		exit 198
	}
} /* end code aggregate linear model raw CRS variables. */


/* Disaggregate double-log model */
else if `disaggrln`sector'' == 1 {
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		scalar define elastSP = _b[ln`sector'SPtotalGDP6]
		scalar define elastTC = _b[ln`sector'TCtotalGDP6]
		mean publicexp`sector'FADGDP `sector'SPtotalGDP6 `sector'TCtotalGDP6 if sample == 1
		scalar define mpublicexp`sector'FADGDP = _b[publicexp`sector'FADGDP]
		scalar define m`sector'SPtotalGDP6 = _b[`sector'SPtotalGDP6]
		scalar define m`sector'TCtotalGDP6 = _b[`sector'TCtotalGDP6]
		scalar define margeffectSP = elastSP*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'SPtotalGDP6)
		scalar define margeffectTC = elastTC*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'TCtotalGDP6)
		estadd scalar margeffectSP: `resultsstorage'
		estadd scalar margeffectTC: `resultsstorage'
		qui xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, fe vce(robust)
		test ln`sector'SPtotalGDP6 = 0
		estadd scalar fullfungtestSP = ttail(r(df_r), _b[ln`sector'SPtotalGDP6]/_se[ln`sector'SPtotalGDP6]): `resultsstorage'
		test ln`sector'SPtotalGDP6 = (1 + m`sector'SPtotalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar nofungtestSP = ttail(r(df_r), - (_b[ln`sector'SPtotalGDP6]-(1 + m`sector'SPtotalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'SPtotalGDP6]): `resultsstorage'
		test ln`sector'TCtotalGDP6 = - (1 + m`sector'TCtotalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar fullfungtestTC = ttail(r(df_r), (_b[ln`sector'TCtotalGDP6] + (1 + m`sector'TCtotalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'TCtotalGDP6]): `resultsstorage'
		test ln`sector'TCtotalGDP6 = 0
		estadd scalar nofungtestTC = ttail(r(df_r), - _b[ln`sector'TCtotalGDP6]/_se[ln`sector'TCtotalGDP6]): `resultsstorage'
		drop sample
	}
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		scalar define elastSP = _b[ln`sector'SPtotalGDP6]
		scalar define elastTC = _b[ln`sector'TCtotalGDP6]
		mean publicexp`sector'FADGDP `sector'SPtotalGDP6 `sector'TCtotalGDP6 if sample == 1
		scalar define mpublicexp`sector'FADGDP = _b[publicexp`sector'FADGDP]
		scalar define m`sector'SPtotalGDP6 = _b[`sector'SPtotalGDP6]
		scalar define m`sector'TCtotalGDP6 = _b[`sector'TCtotalGDP6]
		scalar define margeffectSP = elastSP*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'SPtotalGDP6)
		scalar define margeffectTC = elastTC*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'TCtotalGDP6)
		estadd scalar margeffectSP: `resultsstorage'
		estadd scalar margeffectTC: `resultsstorage'
		qui xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, fe vce(robust)
		test ln`sector'SPtotalGDP6 = 0
		estadd scalar fullfungtestSP = ttail(r(df_r), _b[ln`sector'SPtotalGDP6]/_se[ln`sector'SPtotalGDP6]): `resultsstorage'
		test ln`sector'SPtotalGDP6 = (1 + m`sector'SPtotalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar nofungtestSP = ttail(r(df_r), - (_b[ln`sector'SPtotalGDP6]-(1 + m`sector'SPtotalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'SPtotalGDP6]): `resultsstorage'
		test ln`sector'TCtotalGDP6 = - (1 + m`sector'TCtotalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar fullfungtestTC = ttail(r(df_r), (_b[ln`sector'TCtotalGDP6] + (1 + m`sector'TCtotalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'TCtotalGDP6]): `resultsstorage'
		test ln`sector'TCtotalGDP6 = 0
		estadd scalar nofungtestTC = ttail(r(df_r), - _b[ln`sector'TCtotalGDP6]/_se[ln`sector'TCtotalGDP6]): `resultsstorage'
		/* Generalised Hausman test: */
		xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}
	else {
	di as error "Estimation technique ill-specified"
	exit 198
	}
}

/* Aggregate double-log model */
else if `aggrln`sector'' == 1 { 
	if "`esttechn'" == "OLS" | "`esttechn'" == "FD" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_clust): `resultsstorage'
		estadd scalar R2 = e(r2): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		scalar define elast = _b[ln`sector'totalGDP6]
		mean publicexp`sector'FADGDP `sector'totalGDP6 if sample == 1
		scalar define mpublicexp`sector'FADGDP = _b[publicexp`sector'FADGDP]
		scalar define m`sector'totalGDP6 = _b[`sector'totalGDP6]
		scalar define margeffect = elast*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'totalGDP6)
		estadd scalar margeffect: `resultsstorage'
		qui xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, fe vce(robust)
		test ln`sector'totalGDP6 = 0
		estadd scalar fullfungtest = ttail(r(df_r), _b[ln`sector'totalGDP6]/_se[ln`sector'totalGDP6]): `resultsstorage'
		test ln`sector'totalGDP6 = (1 + m`sector'totalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar nofungtest = ttail(r(df_r), - (_b[ln`sector'totalGDP6]-(1 + m`sector'totalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'totalGDP6]): `resultsstorage'
		drop sample
	}
	else if "`esttechn'" == "FE" {
		capture drop sample
		gen sample = 1 if e(sample)
		estadd scalar Countries = e(N_g): `resultsstorage'
		estadd scalar F_FE = e(F): `resultsstorage'
		estadd scalar R2 = e(r2_w): `resultsstorage'
		test `RHS'
		estadd scalar F_test = r(F): `resultsstorage'
		estadd scalar F_test_p = r(p): `resultsstorage'
		scalar define elast = _b[ln`sector'totalGDP6]
		mean publicexp`sector'FADGDP `sector'totalGDP6 if sample == 1
		scalar define mpublicexp`sector'FADGDP = _b[publicexp`sector'FADGDP]
		scalar define m`sector'totalGDP6 = _b[`sector'totalGDP6]
		scalar define margeffect = elast*(1 + mpublicexp`sector'FADGDP)/(1 + m`sector'totalGDP6)
		estadd scalar margeffect: `resultsstorage'
		qui xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, fe vce(robust)
		test ln`sector'totalGDP6 = 0
		estadd scalar fullfungtest = ttail(r(df_r), _b[ln`sector'totalGDP6]/_se[ln`sector'totalGDP6]): `resultsstorage'
		test ln`sector'totalGDP6 = (1 + m`sector'totalGDP6)/(1 + mpublicexp`sector'FADGDP)
		estadd scalar nofungtest = ttail(r(df_r), - (_b[ln`sector'totalGDP6]-(1 + m`sector'totalGDP6)/(1 + mpublicexp`sector'FADGDP))/_se[ln`sector'totalGDP6]): `resultsstorage'
		/* Generalised Hausman test: */
		xtreg lnpublicexp`sector'FADGDP `RHS' year1991-year2003, re vce(cluster recipientid)
		if e(sigma_u) == 0 {
			estadd scalar Hausmanp = .: `resultsstorage'
			estadd scalar SarganHansen = .: `resultsstorage'				
		}
		else {
			xtoverid
			estadd scalar Hausmanp = r(jp): `resultsstorage'
			estadd scalar SarganHansen = r(j): `resultsstorage'		
		}
		drop sample
	}
	else {
	di as error "Estimation technique ill-specified"
	exit 198
	}
}
else {
	display as error "Are you sure these are the correct RHS variables or that this is the correct model? This is the local for the first variable: `i'"
	exit 198
}
end
